﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace ITask
{
    /// <summary>
    /// 服务
    /// </summary>
    [Serializable]
    public class TaskConfig
    {
        /// <summary>
        /// 服务名称
        /// </summary>
        [XmlAttribute]
        public string name;

        /// <summary>
        /// 服务描述
        /// </summary>
        [XmlAttribute]
        public string description;

        /// <summary>
        /// 服务设置集合
        /// </summary>
        public Setting Settings;

        /// <summary>
        /// 服务保存数据集合
        /// </summary>
        public Saving Savings;

        /// <summary>
        /// 用于序列化的构造式
        /// </summary>
        public TaskConfig() { }

        #region Methods
        /// <summary>
        /// 序列化
        /// </summary>
        public void Serialize(string file)
        {
            XmlSerializer s = new XmlSerializer(typeof(TaskConfig));
            using (System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(file, Encoding.Default))
            {
                try
                {
                    writer.Formatting = System.Xml.Formatting.Indented;
                    s.Serialize(writer, this);
                    writer.Close();
                }
                catch (Exception ex)
                {
                    if (null != writer)
                    {
                        writer.Close();
                    }
                    throw ex;
                }
            }
        }

        /// <summary>
        /// 反序列化
        /// </summary>
        public static TaskConfig Deserialize(string file)
        {
            XmlSerializer mySerializer = new XmlSerializer(typeof(TaskConfig));
            using (System.Xml.XmlReader reader = new System.Xml.XmlTextReader(file))
            {
                try
                {
                    TaskConfig tc = ((TaskConfig)mySerializer.Deserialize(reader));
                    return tc;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (null != reader)
                    {
                        reader.Close();
                    }
                }
            }

        }
        #endregion
    }

    /// <summary>
    /// 服务设定集合
    /// </summary>
    [Serializable]
    public class Setting
    {
        /// <summary>
        /// 是否已上线
        /// </summary>
        public bool IfOnline;

        /// <summary>
        /// 如果失败重试的次数
        /// </summary>
        public int RetryTimes;

        /// <summary>
        /// 配置字符串
        /// </summary>
        public string ConfigString;

        /// <summary>
        /// 用于序列化的构造式
        /// </summary>
        public Setting() { }
    }

    /// <summary>
    /// 服务保存的数据集合
    /// </summary>
    [Serializable]
    public class Saving
    {
        /// <summary>
        /// 上次运行结束的时间
        /// </summary>
        public string LastRunTime;

        /// <summary>
        /// 所需要重新处理的数据集合
        /// </summary>
        [XmlArrayAttribute("Reworks")]
        public Rework[] Reworks;

        /// <summary>
        /// 所需要重新处理的数据集合
        /// </summary>
        [XmlArrayAttribute("Errors")]
        public Error[] Errors;

        /// <summary>
        /// 用于序列化的构造方法
        /// </summary>
        public Saving() { }

        /// <summary>
        /// 构造式
        /// </summary>
        /// <param name="lastRunTime">上次运行结束的时间</param>
        public Saving(string lastRunTime)
        {
            this.LastRunTime = lastRunTime;
        }
    }

    /// <summary>
    /// 要重新运行的数据的标记
    /// </summary>
    [Serializable]
    public class Rework
    {
        /// <summary>
        /// 重试标记
        /// </summary>
        public string ReworkMark;

        /// <summary>
        /// 重试次数
        /// </summary>
        public int TriedTimes;

        /// <summary>
        /// 用于序列化的构在式
        /// </summary>
        public Rework() { }

        /// <summary>
        /// 构造式
        /// </summary>
        /// <param name="rework">要重新运行的数据的标记</param>
        /// /// <param name="triedTimes">要重新运行的次数</param>
        public Rework(string rework, int triedTimes) { this.ReworkMark = rework; this.TriedTimes = triedTimes; }

    }

    /// <summary>
    /// 错误且不再运行的数据的标记
    /// </summary>
    [Serializable]
    public class Error
    {
        /// <summary>
        /// 重试标记
        /// </summary>
        public string ErrorMark;

        /// <summary>
        /// 用于序列化的构在式
        /// </summary>
        public Error() { }

        /// <summary>
        /// 构造式
        /// </summary>
        /// <param name="errorMark">错误数据的标记</param>
        public Error(string errorMark) { this.ErrorMark = errorMark; }
    }
}
